package server;

import static org.junit.Assert.*;
import logic.BookItem;
import logic.LendReport;
import logic.Message;
import logic.Reader;

import org.junit.Before;
import org.junit.Test;

public class DBhandlerTest {

	DBhandler db;
	Message m;
	@Before
	public void setUp() throws Exception {
		
	}

	@Test
	public void testGetLendReport() {
		try {
			db=new DBhandler();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		testWanted();
		testRegular();
	}
	
	private void testRegular() {
		
		m=db.GetLendReport();
		
		BookItem b = new BookItem(0);
		Reader r = new Reader();
		b.setCatalogNumber(2232371);
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		m=db.GetLendReport();
		LendReport after=(LendReport)m.getObjects();
		
		//check that average changed
		assertEquals(6, after.getRegularAvg());
		
		//check that median change
		assertEquals(3, after.getRegularMedian());
		
		
	}

	public void testWanted(){
		m=db.GetLendReport();
		
		BookItem b = new BookItem(0);
		Reader r = new Reader();
		b.setCatalogNumber(2232375);
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		r.setReaderId(250);
		db.Lend(r, b);
		db.ReturnBook(r, b, "");
		
		m=db.GetLendReport();
		LendReport after=(LendReport)m.getObjects();
		
		//check that average changed
		assertEquals(2, after.getWantedAvg());
		
		//check that median change
		assertEquals(1, after.getWantedMedian());
		
	}

	
}
